package com.bookbuf.cardflipanimation;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;
import android.widget.TabWidget;

import com.bookbuf.cardflipanimation.sample.Fragment111;
import com.bookbuf.cardflipanimation.sample.Fragment222;
import com.bookbuf.cardflipanimation.sample.Fragment333;
import com.bookbuf.cardflipanimation.sample.Fragment444;

/**
 * Created by robert on 16/7/6.
 */
public class CardFrontFragment extends Fragment {

	public static final String TAG_CUSTOMER = "indicator"; //指标
	public static final String TAG_INCOME = "document";    //档案
	public static final String TAG_DISCOVER = "discover";  //发现
	public static final String TAG_MINE = "more";          //更多

	private static final String BUNDLE_POSITION = "position";

	RetainInstanceFragmentTabHost tabHost;
	int position = 0;/*default 0*/

	public static CardFrontFragment newInstance () {

		Bundle args = new Bundle ();

		CardFrontFragment fragment = new CardFrontFragment ();
		fragment.setArguments (args);
		return fragment;
	}

	@Override
	public View onCreateView (LayoutInflater inflater, ViewGroup container,
							  Bundle savedInstanceState) {
		return inflater.inflate (R.layout.fragment_face, container, false);
	}

	@Override
	public void onViewCreated (View view, @Nullable Bundle savedInstanceState) {
		super.onViewCreated (view, savedInstanceState);
		if (savedInstanceState == null) {
			inflateMainTabs (view);
		}
		tabHost.setCurrentTab (position);

	}

	private TabHost.TabSpec createTabSpec (TabHost host, String tag, int viewResId) {

		LayoutInflater inflater = getActivity ().getLayoutInflater ();
		View indicatorView = inflater.inflate (viewResId, null);
		TabHost.TabSpec tabSpec = host.newTabSpec (tag);
		tabSpec.setIndicator (indicatorView);
		return tabSpec;
	}

	private void inflateMainTabs (View view) {


		Log.e ("RetainInstanceTabHost", "inflateMainTabs: +");

		tabHost = (RetainInstanceFragmentTabHost) view.findViewById (android.R.id.tabhost);
		tabHost.setup (getActivity (), getFragmentManager (), R.id.realtabcontent);

		TabWidget tabWidget = tabHost.getTabWidget ();
		tabWidget.setBackgroundResource (android.R.color.white);
		tabWidget.setDividerDrawable (android.R.color.transparent);


		tabHost.addTab (createTabSpec (tabHost, TAG_CUSTOMER, R.layout.tab_111), Fragment111.class, null);
		tabHost.addTab (createTabSpec (tabHost, TAG_INCOME, R.layout.tab_222), Fragment222.class, null);
		tabHost.addTab (createTabSpec (tabHost, TAG_DISCOVER, R.layout.tab_333), Fragment333.class, null);
		tabHost.addTab (createTabSpec (tabHost, TAG_MINE, R.layout.tab_444), Fragment444.class, null);


		tabHost.setOnTabChangedListener (new TabHost.OnTabChangeListener () {
			@Override
			public void onTabChanged (String tabId) {
				switch (tabId) {
					case TAG_CUSTOMER:
						position = 0;
						break;
					case TAG_INCOME:
						position = 1;
						break;
					case TAG_DISCOVER:
						position = 2;
						break;
					case TAG_MINE:
						position = 3;
						break;
					default:
						break;
				}
			}
		});

		Log.e ("RetainInstanceTabHost", "inflateMainTabs: -");
	}
}
